package com.lqk.demo.concurrencytools.threadpool.juc.cyclicbarrier;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.lqk.demo.n2.util.Sleeper.sleep;

/**
 * @author lqk
 * @date 2022/1/19 17:25
 */
@Slf4j
public class TestCyclicBarrier {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 3; i++) {
            CountDownLatch latch = new CountDownLatch(2);
            service.submit(()->{
                log.debug("task1 start ...");
                sleep(1);
                latch.countDown();
            });

            service.submit(()->{
                log.debug("task2 start ...");
                sleep(2);
                latch.countDown();
            });
            latch.await();
            log.debug("task1 task2 finish ...");
        }
        service.shutdown();
    }
}
